<%-- 
    Document   : index
    Created on : 18-nov-2013, 11.35.34
    Author     : Ilo
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html dir="ltr" lang="it-IT">
    <head>
        <title>EvenTO::Registrati</title>

        <jsp:include page="html/metadata.jsp"/>

        <script lang="javascript">
            $(function() {
                    // [ BITHDATE] //
                    for (i = new Date().getFullYear(); i > 1900; i--)
                    {
                        $('#years').append($('<option />').val(i).html(i));
                    }

                    for (i = 1; i < 13; i++)
                    {
                        //nel formato standard sarebbe January=0, Feb=1,..
                        $('#months').append($('<option />').val(i).html(i));
                    }

                    updateNumberOfDays();

                    $('#years, #months').change(function() {
                        updateNumberOfDays();
                    });

                    function updateNumberOfDays() {
                        //$('#days').html('<option selected disabled>day</option>');
                        month = $('#months').val();
                        year = $('#years').val();
                        days = daysInMonth(month, year);

                        for (i = 1; i < days + 1; i++) {
                            $('#days').append($('<option />').val(i).html(i));
                        }

                    }

                    function daysInMonth(month, year) {
                        return new Date(year, month, 0).getDate();
                    }                                   
                     // [ /BITHDATE] //
                     

                    $('#insertUser').click(function(e) {
                        e.preventDefault();
                                     
                        /* [CONTROLLI] */
                        var check = true;
                        var err="";
                        if ($('input[name="name"]').val() === "") {
                            check = false;
                            err+="<li>Inserisci un nome valido</li>";
                        }
                        if ($('input[name="surname"]').val() === "") {
                            check = false;
                            err+="<li>Inserisci cognome valido</li>";
                        }
                        if ($('input[name="username"]').val() === "") {
                            check = false;
                            err+="<li>Inserisci username valido</li>";
                        }
                        if ($('input[name="pwd1"]').val() === "") {
                            check = false;
                            err+="<li>Inserisci una password valida</li>";
                        }
                        if ($('input[name="pwd1"]').val() !== $('input[name="pwd2"]').val()) {
                            check = false;
                            err+="<li>Le tue password sono diverse</li>";
                        }
                        if($('#years').val()===null || $('#months').val()===null || $('#days').val()===null){
                            check = false;
                            err+="<li>Inserisci una data di nascita valida</li>";
                        }
                        
                        $('#outputMsg').html("<ul>"+err+"</ul>");
                        //(alert("years"+$('#years').val()+" month: "+ $('#months').val()+"days: "+$('#days').val());
                        
                        /* [CONTROLLI] */                        
                        

                        if (check) {  // -> controlli superati
                            $.post("Controller",
                                    {type: "insertUser",
                                        name: $('input[name="name"]').val(),
                                        surname: $('input[name="surname"]').val(),
                                        birthY: $('#years').val(),
                                        birthM: $('#months').val(),
                                        birthD: $('#days').val(),
                                        company: $('input[name="company"]').val(),
                                        info: $('input[name="info"]').val(),
                                        username: $('input[name="username"]').val(),
                                        pwd1: $.sha1($('input[name="pwd1"]').val()),
                                        publisher: $('#publisher').is(':checked')
                                    },
                                    function(data) {
                                        if(data.status === 0) {
                                            $('#outputMsg').html("<ul>"+data.message+"</ul>");
                                            window.location.replace("index.jsp"); //spara utente in index
                                        }
                                        else{
                                            $('#outputMsg').html("<ul>"+data.message+"</ul>");
                                        }
                                    },
                                    'json');
                        }
                    });
                });
        </script>
    </head>
    <body>
        <div id="evento-main">
            <!-- header -->
            <jsp:include page="html/header.jsp" />
            <!-- /header -->

            <!-- top-menu -->
            <%@include file="html/top-menu.jsp" %>
            <!-- /top-menu -->

            <div class="evento-sheet clearfix">
                <div class="evento-layout-wrapper">
                    <div class="evento-content-layout">
                        <div class="evento-content-layout-row">
                            <div class="evento-layout-cell evento-content">
                                <article class="evento-post evento-article">
                                    <div class="evento-postmetadataheader">
                                        <h2 class="evento-postheader">Registrati</h2>
                                    </div>

                                    <div class="evento-postcontent evento-postcontent-0 clearfix">
                                        <form id="frmInsertEvent">
                                            <div class="input-group input-group-sm">
                                                <span class="input-group-addon">&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                                <input type="text" name="name" class="form-control" placeholder="Nome" alt="Nome" />
                                            </div>
                                            <br>
                                            <div class="input-group input-group-sm">
                                                <span class="input-group-addon">&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                                <input type="text" name="surname" class="form-control" placeholder="Cognome" alt="Cognome" />
                                            </div>
                                            <br>
                                            Data di Nascita:
                                            <span>
                                                <select id="years">
                                                    <option selected disabled>anno</option>
                                                </select>
                                                <select id="months">
                                                    <option selected disabled>mese</option>
                                                </select>
                                                <select id="days">
                                                    <option selected disabled>giorno</option>
                                                </select>
                                            </span>
                                            <br><br>
                                            <div class="input-group input-group-sm">
                                                <span class="input-group-addon">&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                                <input type="text" name="company" class="form-control" placeholder="Nome Ditta (facoltativo)" alt="Nome Ditta (facoltativo)" />
                                            </div>
                                            <br>
                                            <div class="input-group input-group-sm">
                                                <span class="input-group-addon">&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                                <input type="text" name="info" class="form-control" placeholder="Informazioni su di te (facoltativo)" alt="Informazioni su di te (facoltativo)" />
                                            </div>
                                            <br>
                                            <div class="input-group input-group-sm">
                                                <span class="input-group-addon">&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                                <input type="text" name="username" class="form-control" placeholder="Username" alt="Username" />
                                            </div>
                                            <br>
                                            <div class="input-group input-group-sm">
                                                <span class="input-group-addon">&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                                <input type="text" name="pwd1" class="form-control" placeholder="Password" alt="Password" />
                                            </div>
                                            <br>
                                            <div class="input-group input-group-sm">
                                                <span class="input-group-addon">&nbsp;&nbsp;&nbsp;&nbsp;</span>
                                                <input type="text" name="pwd2" class="form-control" placeholder="Conferma Password" alt="Conferma Password" />
                                            </div>
                                            <br>
                                            <input type="checkbox" name="publisher" value="publisher" id="publisher"/> Voglio caricare eventi
                                            <br><br>
                                            <input type="button" id="insertUser" value="Registrami" class="evento-button" />
                                        </form>
                                        <div id="outputMsg"><ul></ul></div>
                                    </div>
                                </article>
                            </div>

                            <!-- sidebar -->
                            <jsp:include page="html/sidebar.jsp" />
                            <!-- /sidebar -->

                        </div>
                    </div>
                </div>

                <!-- footer -->
                <%@include file="html/footer.jsp" %>
                <!-- /footer -->

            </div>
        </div>
    </body>
</html>
